﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;


namespace Engine
{
    public class Animation
    {
        Texture2D mTexture;
        public float rotation;
        public Vector2 mPosition;
        int countX, countY, total, actualFrame, mSpeed,timeCounter;
        public Animation(Texture2D texture, int countx, int county,int t,int speed,Vector2 position)
        {
            mTexture = texture;
            countX = countx;
            countY = county;
            actualFrame = 0;
            total = t;
            mSpeed = speed;
            mPosition = position;
            timeCounter = 0;
            rotation = 0;
        }
        public Rectangle GetCurrentRect()  //Rectangle x,y,ancho,alto
        {
            Rectangle currentFrame;
            currentFrame.Width = mTexture.Width / countX;
            currentFrame.Height = mTexture.Height / countY;
            currentFrame.X = currentFrame.Width * (actualFrame % countX);
            currentFrame.Y = currentFrame.Height * (actualFrame / countX);

            return currentFrame;
        }
        public Vector2 GetOrigin()
        {
            return new Vector2((mTexture.Width / countX) / 2, (mTexture.Height / countY) / 2);
        }
        public void Update(int time)
        {
            timeCounter += time;
            if (timeCounter > mSpeed)//speed es cuanto va a pasar entre frame y frame de la animacion
            {
                actualFrame++;
                timeCounter -= mSpeed;
            }
            if (actualFrame >= total)
                actualFrame = 0;

        }
        public void Draw(SpriteBatch spriteBatch)
        {
           
            spriteBatch.Draw(mTexture, mPosition, GetCurrentRect(),
                Color.White, rotation, GetOrigin(),1, SpriteEffects.None, 0);
        }
        public Texture2D GetTexture() { return mTexture; }

    }
}
